<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>服务产品列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<form class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label">产品类别</label>
            <div class="layui-input-block">
                <select name="categoryId">
                	<option value="">--请选择产品类别--</option>
				</select>
            </div>
        </div>
        
        <div class="layui-inline">
            <label class="layui-form-label">产品名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="name" placeholder="请输入产品名称">
            </div>
        </div>
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit="" lay-filter="queryProduct" style="background: #fff;color:#5785db;border:1px solid #5785db;">查询</a>
        </div>
        <div class="layui-inline dynamicShow">
           <a class="layui-btn layui-btn-normal productsAdd_btn">添加产品</a>
        </div>
    </form>
		
	</blockquote>
	<div class="layui-form news_list">
	  	<table class="layui-table" id="productList" lay-filter="products"></table>
	</div>
	<div id="page"></div>
	<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
	<script type="text/html" id="barDemo">
		  <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>编辑</a>
		  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
  	</script>
	<script type="text/javascript">
	layui.use(['table','form'], function(){
	    var table = layui.table;
	    var form = layui.form;

        var isAdminstr=localStorage.getItem("isAdmin");
        var isAdmin=isAdminstr=='true'?true:false;
        if(isAdmin){
            $(".dynamicShow").hide();
        }else{
            $(".dynamicShow").show();
        }

	    var tableIns=table.render({
	        elem: '#productList'
	        ,url: '/products'
	        ,cols: [[
	            {field:'categoryName', title: '产品类别',width:"10%"}
//	            ,{field:'categoryName', title: '产品类别',width:"10%"}
	            ,{field:'name', title: '产品名称',width:"10%" }
	            ,{field:'price', title: '产品价格',width:"10%"}
                ,{field:'consumeTime', title: '平均耗时(分钟)',width:"10%"}
                ,{field:'procedureCount', title: '工序数',width:"10%"}
	            ,{field:'description', title: '产品说明',width:"10%"}
	            ,{title: '操作', align:'center', toolbar: '#barDemo',width:"25%"}
	        ]]
	        ,id: 'testReload'
	        ,page: true
	    });
	    
	    
	    form.on("submit(queryProduct)", function (data) {
	        var categoryId = data.field.categoryId;
	        var name = data.field.name;
	        tableIns.reload({
	            where:{
	            	categoryId:categoryId,
	            	name:name
	            }
	        });
	        return false;
	    });
	    
	    table.on('tool(products)', function(obj){
	    	  var layEvent = obj.event;
			var ids=obj.data.id;
				if(layEvent === 'del'){ //删除
	    	    layer.confirm('真的删除该服务产品吗', function(index){
	    	      layer.close(index);

	    	      $.ajax({  
	    	            type: "DELETE",
	    	            url:"/products/delete/"+ids,
	    	            success: function(res) {
	    	            	if(res.code==2000){
                                obj.del();
                                top.layer.close(index);
	    	          			top.layer.msg("删除成功");
	    	            	}else{
	    	            		top.layer.msg(res.msg);
	    	            	}
						} ,
					 	error: function(res) {
                            layer.msg("请求错误");
					 	},
					  	beforeSend:function () {
	    	                var token="Bearer eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1MzU5MTU4NTYsInVzZXJuYW1lIjoiY2VzaGkxIn0.NTzGLVxbHgYyrZjSWYm3RJw1vbFJpQUXUGs7h4kYBw_oSa35OTiGpO1bBG180DWCYBuFpdSEmHnzhFE-dWrNrw";

							
                        }
	    	        }); 
	    	    });
	    	  } else if(layEvent === 'edit'){ //编辑
	    	     editProduct(ids);
	    	  }
	    	});


        var categoryList;
        $.ajax({
            type : "GET",
            url: 'categorys/getAllCategorys',
            dataType:'json',
            async:false,
            success: function(data){
                var list=data.data;
                categoryList=list;
                var html='';
                for(var i=0;i<list.length;i++){
                    html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>"
                }
                $("select[name='categoryId']").append(html);
                form.render();
            }
        });


        function editProduct(id){
            var index = layui.layer.open({
                title: "编辑服务产品",
                type: 2,
                area:["100%","100%"],
                content: "products/productsEdit.html?id="+id
            });
        }

        $(".productsAdd_btn").click(function(){
            var index = layui.layer.open({
                title : "添加服务产品",
                area:["100%","100%"],
                type : 2,
                content : "products/productsAdd.html"
            })
        })


    });
	



	

	</script> 
</body>
</html>